package com.ruoyi.business.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.domain.BusinessMessageTemplate;
import com.ruoyi.business.service.IBusinessMessageTemplateService;

/**
 * 消息模板管理 信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/business/template")
public class MessageTemplateController extends BaseController
{
    private String prefix = "business/template";

    @Autowired
    private IBusinessMessageTemplateService businessMessageTemplateService;

    @RequiresPermissions("business:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询消息模板列表
     */
    @RequiresPermissions("business:template:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessMessageTemplate businessMessageTemplate)
    {
        startPage();
        List<BusinessMessageTemplate> list = businessMessageTemplateService.selectBusinessMessageTemplateList(businessMessageTemplate);
        return getDataTable(list);
    }

    /**
     * 导出消息模板列表
     */
    @RequiresPermissions("business:template:export")
    @Log(title = "消息模板管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessMessageTemplate businessMessageTemplate)
    {
        List<BusinessMessageTemplate> list = businessMessageTemplateService.selectBusinessMessageTemplateList(businessMessageTemplate);
        ExcelUtil<BusinessMessageTemplate> util = new ExcelUtil<BusinessMessageTemplate>(BusinessMessageTemplate.class);
        return util.exportExcel(list, "消息模板数据");
    }

    /**
     * 新增消息模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息模板
     */
    @RequiresPermissions("business:template:add")
    @Log(title = "消息模板管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated BusinessMessageTemplate businessMessageTemplate)
    {
        if (UserConstants.TEMPLATE_CODE_NOT_UNIQUE.equals(businessMessageTemplateService.checkTemplateCodeUnique(businessMessageTemplate)))
        {
            return error("新增模板'" + businessMessageTemplate.getTemplateName() + "'失败，模板代码已存在");
        }
        return toAjax(businessMessageTemplateService.insertBusinessMessageTemplate(businessMessageTemplate));
    }

    /**
     * 修改消息模板
     */
    @RequiresPermissions("business:template:edit")
    @GetMapping("/edit/{templateId}")
    public String edit(@PathVariable("templateId") Long templateId, ModelMap mmap)
    {
        BusinessMessageTemplate businessMessageTemplate = businessMessageTemplateService.selectBusinessMessageTemplateById(templateId);
        mmap.put("template", businessMessageTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息模板
     */
    @RequiresPermissions("business:template:edit")
    @Log(title = "消息模板管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated BusinessMessageTemplate businessMessageTemplate)
    {
        if (UserConstants.TEMPLATE_CODE_NOT_UNIQUE.equals(businessMessageTemplateService.checkTemplateCodeUnique(businessMessageTemplate)))
        {
            return error("修改模板'" + businessMessageTemplate.getTemplateName() + "'失败，模板代码已存在");
        }
        return toAjax(businessMessageTemplateService.updateBusinessMessageTemplate(businessMessageTemplate));
    }

    /**
     * 删除消息模板
     */
    @RequiresPermissions("business:template:remove")
    @Log(title = "消息模板管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(businessMessageTemplateService.deleteBusinessMessageTemplateByIds(ids));
    }
    
    /**
     * 校验模板代码
     */
    @PostMapping("/checkTemplateCodeUnique")
    @ResponseBody
    public String checkTemplateCodeUnique(BusinessMessageTemplate businessMessageTemplate)
    {
        return businessMessageTemplateService.checkTemplateCodeUnique(businessMessageTemplate);
    }
    
    /**
     * 模板状态修改
     */
    @Log(title = "消息模板管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("business:template:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(BusinessMessageTemplate businessMessageTemplate)
    {
        return toAjax(businessMessageTemplateService.changeStatus(businessMessageTemplate));
    }
    
    /**
     * 查看消息模板详情
     */
    @RequiresPermissions("business:template:detail")
    @GetMapping("/detail/{templateId}")
    public String detail(@PathVariable("templateId") Long templateId, ModelMap mmap)
    {
        BusinessMessageTemplate businessMessageTemplate = businessMessageTemplateService.selectBusinessMessageTemplateById(templateId);
        mmap.put("template", businessMessageTemplate);
        return prefix + "/detail";
    }
} 